
package com.gw.business.statistics.detailed.controller;

import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gw.business.analysis.model.UserTrackDataDetail;
import com.gw.business.analysis.service.UserTrackDataDetailService;
import com.gw.business.common.util.DateUtil;
import com.gw.business.common.util.excel.ConditionCollection;
import com.gw.business.common.util.excel.ExcelOperate;
import com.gw.business.statistics.detailed.model.DetailForm;
import com.gw.platform.controller.DefaultPageController;
import com.gw.platform.page.impl.Page;
import com.gw.platform.util.StringUtils;



/**
 * 详细汇总
 * 
 * @author kidy
 * @since 1.0
 * @version 2015年2月2日 kidy
 */

@Controller
@RequestMapping("detail")
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
public class DetailController extends DefaultPageController {
    final static Logger logger  = Logger.getLogger(DetailController.class);
    @Resource
    private UserTrackDataDetailService userTrackDataDetailService;
    
    private ExcelOperate excelOperate;
    
    /**
     * 详情列表
     * 
     * @return
     */
    @RequestMapping("/page")
    public String queryDetail(DetailForm detailForm) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("numPerPage");
        String temp =(String) request.getParameter("temp");
        String dafaultBeginTime =(String) request.getParameter("dafaultBeginTime");
        String endBeginTime =(String) request.getParameter("endBeginTime");
        
        if (StringUtils.isEmpty(pageNum)) {
            pageNum = "1";
            pageSize = Page.DEFAULT_PAGE_SIZE + "";
        }
       if(temp==null){
           detailForm=new DetailForm();
           dafaultBeginTime=DateUtil.getDateFormat((DateUtil.addHours(new Date(), -1)), "yyyy-MM-dd HH:mm:ss");
           endBeginTime=DateUtil.getDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
           detailForm.setStartTime(dafaultBeginTime);
           detailForm.setEndTime(endBeginTime);
       }
        // 查询用户列表
        Page pageData = userTrackDataDetailService.queryUserList(detailForm, pageNum, pageSize);
        request.setAttribute("page", pageData);
        request.setAttribute("form", detailForm);
        request.setAttribute("dafaultBeginTime", dafaultBeginTime);
        request.setAttribute("endBeginTime", endBeginTime);
        return "detail/page";
    }
    
    /**
     * 导出
     * 
     * @param req
     * @param response
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping("/export")
    public void export(HttpServletRequest req, HttpServletResponse response, DetailForm detailForm) {
        OutputStream out = null;
        try {
           
            String pageNum = request.getParameter("pageNum");
            String pageSize = request.getParameter("numPerPage");
            String flag=  request.getParameter("flag");
            Page pageData=null;
            String fileName = "详细列表";
            if(null!=flag){
                fileName = "注册异常";
              detailForm.setRegistException("exception");
            }
            String fileName2 = new String(fileName.getBytes("gb2312"), "iso-8859-1");
            // 文件名
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            response.setHeader("content-disposition", "attachment;filename=" + fileName2 + ".xls");
            
            if (StringUtils.isEmpty(pageNum)) {
                pageNum = "1";
                pageSize = Page.DEFAULT_PAGE_SIZE + "";
            }
         
           
            pageData = userTrackDataDetailService.queryUserList(detailForm, pageNum, pageSize);
            excelOperate = new ExcelOperate();
            excelOperate.setDataList((List<UserTrackDataDetail>) pageData.getData());
            excelOperate.setThAndFieldMap(ConditionCollection.getCommuDetailTHFieldMap(flag));
            excelOperate.excelExportTemp(out, response);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.flush();
                out.close();
            } catch (Exception e2) {
                
            }
        }
        
    }
    /**
     * 用户详情页面
     * @return
     */
    @RequestMapping("/dataDetail")
    public String dataDetail(){
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("numPerPage");
        String userId = request.getParameter("userId");
        if(StringUtils.isEmpty(pageNum)){
            pageNum = "1";
            pageSize = Page.DEFAULT_PAGE_SIZE + "";
        }
     // 设置当前页
        setCurrent(Long.parseLong(pageNum));
        Map<String, Object> map = new HashMap<String, Object>();
        if (userId!=null&&userId!="") {
            map.put("userId", userId);
          
        }  
        // 查询用户列表
        page = defaultService.page("queryUserDetailList", "queryUserDetailCount", getCurrent(), map, Integer.parseInt(pageSize));
       
       
        request.setAttribute("page", page);
        request.setAttribute("userId", userId);
        return "detail/detail";
        
    }
    /**
     * 导出
     * 
     * @param req
     * @param response
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping("/detailExport")
    public void detailExport(HttpServletRequest req, HttpServletResponse response, DetailForm detailForm) {
        OutputStream out = null;
        try {
            String pageNum = request.getParameter("pageNum");
            String pageSize = request.getParameter("numPerPage");
            String userId = request.getParameter("userId");
            String fileName ="用户"+userId+"详细列表";
            String fileName2 = new String(fileName.getBytes("gb2312"), "iso-8859-1");
            // 文件名
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            response.setHeader("content-disposition", "attachment;filename=" + fileName2 + ".xls");
            
       
            if(StringUtils.isEmpty(pageNum)){
                pageNum = "1";
                pageSize = Page.DEFAULT_PAGE_SIZE + "";
            }
         // 设置当前页
            setCurrent(Long.parseLong(pageNum));
            Map<String, Object> map = new HashMap<String, Object>();
            if (userId!=null&&userId!="") {
                map.put("userId", userId);
              
            } 
            // 查询用户列表
            page = defaultService.page("queryUserDetailList", "queryUserDetailCount", getCurrent(), map, Integer.parseInt(pageSize));
            
            excelOperate = new ExcelOperate();
            excelOperate.setDataList((List<UserTrackDataDetail>) page.getData());
            excelOperate.setThAndFieldMap(ConditionCollection.getCommuDetailTHFieldMap(null));
            excelOperate.excelExportTemp(out, response);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.flush();
                out.close();
            } catch (Exception e2) {
                
            }
        }
        
    }
    /**
     * 注册异常
     * 
     * @return
     */
    @RequestMapping("/registException")
    public String queryRegistException(DetailForm detailForm) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("numPerPage");
        String regEptionBeginTime =(String) request.getParameter("regEptionBeginTime");
        String regEtionEndTime =(String) request.getParameter("regEtionEndTime");
        String regTemp = (String)request.getParameter("regTemp");
        if(StringUtils.isEmpty(pageNum)){
            pageNum = "1";
            pageSize = Page.DEFAULT_PAGE_SIZE + "";
        }
        if(null==regTemp){
            regEptionBeginTime=DateUtil.getDateFormat((DateUtil.addHours(new Date(), -1)), "yyyy-MM-dd HH:mm:ss");
            regEtionEndTime=DateUtil.getDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
            detailForm.setStartTime(regEptionBeginTime);
            detailForm.setEndTime(regEtionEndTime);
       
        }
        detailForm.setRegistException("exception");
        // 查询用户列表
        Page pageData = userTrackDataDetailService.queryUserList(detailForm, pageNum, pageSize);
        request.setAttribute("page", pageData);
        request.setAttribute("regEptionBeginTime", regEptionBeginTime);
        request.setAttribute("regEtionEndTime", regEtionEndTime);
        request.setAttribute("form", detailForm);
        return "exception/registException/page";
    }
}
